﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{   
    [Serializable]
    public sealed class LopaProject{
        //private ProjectDetails _projectDetails;
        private List<string> _psiList = new List<string>();
        private NewProjectDetails _projectDetails = null;
        private List<PID> _pidList = new List<PID>();
        private List<LOPAWorkNode> _lopaWorkNodeList = new List<LOPAWorkNode>();
        private List<HazopTeam> _lopaTeamList = new List<HazopTeam>();
        private List<CauseEventFrequenceItem> _causeEventList = new List<CauseEventFrequenceItem>();
        private RiskCriteria _riskCriteria = new RiskCriteria();
        private List<IPLItem> _iplList = new List<IPLItem>();
        private static LopaProject _lopaProjectInstance = null;
        private static object syncObj = new object();

        public LopaProject(){
            _iplList = LoadIPIDefaultData();
            _causeEventList = LoadEventDefaultData();
        }

        public List<IPLItem> LoadIPIDefaultData()
        {
            List<IPLItem> list = new List<IPLItem>();
            list.Add(new IPLItem("围堰", "可以降低储罐满罐、破裂或泄漏时发生大范围流散严重后果的频率", "0.01"));
            list.Add(new IPLItem("地下排放系统", "可以降低储罐满罐、破裂或泄漏时发生大范围流散严重后果的频率", "0.01"));
            list.Add(new IPLItem("放空管(管道上没有阀门)", "能避免超压", "0.01"));
            list.Add(new IPLItem("防火层", "减少热的传导并为泄压及消防灭火提供更多的时间", "0.01"));
            list.Add(new IPLItem("防爆墙或掩体", "可以限制爆炸能量和保护设备或建筑物等，以降低爆炸发生导致严重后果的频率", "0.001"));
            list.Add(new IPLItem("本质安全的设计", "如果适当地采用，可以明显地降低相关假想事故情形的频率（注：有些公司通过本质安全设计来消除某些假想的事故情形，例如容器的设计压力高于可能出现的最高压力）", "0.01"));
            list.Add(new IPLItem("阻火器或隔爆器", "如果设计得当，并正确安装和维护，它们可以防止从管道回火至容器或储罐", "0.01"));
            list.Add(new IPLItem("泄压阀", "防止系统超压。它们的有效性与所使用的介质及经验密切相关", "0.01"));
            list.Add(new IPLItem("爆破片", "防止系统超压。它们的有效性与所使用的介质及经验密切相关。", "0.01"));
            list.Add(new IPLItem("基本的工艺控制系统", "如果与初始事件没有关联，可以视为独立保护层", "0.1"));
            list.Add(new IPLItem("完全相同的冗余设备", "冗余的设备，能自动启动或者操作人员可以在要求的时间内启动", "0.1"));
            list.Add(new IPLItem("安全完整性等级 SIL-1", "通常包括：单一传感器（满足故障要求的冗余）单一的逻辑处理器（满足故障要求的冗余）单一的终端元件（满足故障要求的冗余）", "≥0.01 - <0.1"));
            list.Add(new IPLItem("安全完整性等级 SIL-2", "通常包括：多重传感器（满足故障要求的冗余）多途径的逻辑处理器（满足故障要求的冗余）多重终端元件（满足故障要求的冗余）", "≥0.001 - <0.01"));
            list.Add(new IPLItem("安全完整性等级 SIL-3", "通常包括：多重传感器多途径的逻辑处理器）多重终端元件", "≥0.0001 - <0.001"));
            list.Add(new IPLItem("安全完整性等级 SIL-4", "在标准IEC61508和61511中虽然有这类安全仪表功能的要求，但是它们难以设计和维护，所以在LOPA中不考虑", "≥0.00001 - <0.0001"));
            list.Add(new IPLItem("操作人员在10分钟内响应", "有明确的书面规定该采取的响应行动", "0.1"));
            list.Add(new IPLItem("操作人员在40分钟内对工艺控制系统的指示或报警采取响应行动", "有明确的书面规定该采取的响应行动", "0.1"));
            list.Add(new IPLItem("操作人员在40分钟内采取响应行动", "有明确的书面规定该采取的响应行动注：设想有充分的书面资料、培训和验证程序", "0.1"));
            return list;
        }

        public List<CauseEventFrequenceItem> LoadEventDefaultData()
        {
            List<CauseEventFrequenceItem> list = new List<CauseEventFrequenceItem>();
            list.Add(new CauseEventFrequenceItem("压力容器因为余压破裂", "0.000001", "每年"));
            list.Add(new CauseEventFrequenceItem("管道因为余压破裂（100米管道内出现完全破裂）", "0.00001", "每年"));
            list.Add(new CauseEventFrequenceItem("管道泄漏（10%的截面泄漏-100米", "0.001", "每年"));
            list.Add(new CauseEventFrequenceItem("常压储罐破裂", "0.001", "每年"));
            list.Add(new CauseEventFrequenceItem("垫片或密封填料破裂喷出", "0.01", "每年"));
            list.Add(new CauseEventFrequenceItem("透平机或柴油机超速并伴随壳体破裂", "0.0001", "每年"));
            list.Add(new CauseEventFrequenceItem("第三方干扰（挖土机和车辆等的外部撞击）", "0.01", "每年"));
            list.Add(new CauseEventFrequenceItem("起重机重物坠落", "0.0001", "每次操作"));
            list.Add(new CauseEventFrequenceItem("遭受雷击", "0.001", "每年"));
            list.Add(new CauseEventFrequenceItem("安全阀意外起跳", "0.01", "每年"));
            list.Add(new CauseEventFrequenceItem("冷却水故障", "0.1", "每年"));
            list.Add(new CauseEventFrequenceItem("泵的密封破裂", "0.1", "每年"));
            list.Add(new CauseEventFrequenceItem("卸料或装料软管破裂", "0.1", "每年"));
            list.Add(new CauseEventFrequenceItem("工艺的基本控制之仪表回路故障 备注: IEC 61511 要求的限值是大于 1 × 10–5/hr 或 8.76 × 10–2/yr (IEC, 2001)", "0.1", "每年"));
            list.Add(new CauseEventFrequenceItem("调节器故障", "0.1", "每年"));
            list.Add(new CauseEventFrequenceItem("小型外部着火（各种原因综合）", "0.1", "每年"));
            list.Add(new CauseEventFrequenceItem("大型外部着火（各种原因综合）", "0.01", "每年"));
            list.Add(new CauseEventFrequenceItem("上锁挂牌程序执行失效", "0.001", "每次操作"));
            list.Add(new CauseEventFrequenceItem("操作人员失误（执行日常的工作程序，设想接受过良好的培训、没有紧张和疲劳的状态下）", "0.01", "每次操作"));
            return list;
        }

        public static LopaProject LopaProjectInstance {
            get {
                if (_lopaProjectInstance == null) {
                    lock (syncObj) {
                        if (_lopaProjectInstance == null) {
                            _lopaProjectInstance = new LopaProject();
                        }
                    }
                }
                return _lopaProjectInstance;
            }
        }

        public NewProjectDetails ProjectDetails{
            get { return _projectDetails; }
            set { _projectDetails = value; }
        }

        public List<string> PsiList{
            get { return _psiList; }
            set { _psiList = value; }
        }

        public List<PID> PidList{
            get { return _pidList; }
            set { _pidList = value; }
        }

        public List<LOPAWorkNode> LopaWorkNodeList{
            get { return _lopaWorkNodeList; }
            set { _lopaWorkNodeList = value; }
        }

        public List<HazopTeam> LopaTeamList{
            get { return _lopaTeamList; }
            set { _lopaTeamList = value; }
        }

        public List<CauseEventFrequenceItem> CauseEventList{
            get { return _causeEventList; }
            set { _causeEventList = value; }
        }

        public RiskCriteria RiskCriteria{
            get { return _riskCriteria; }
            set { _riskCriteria = value; }
        }

        public List<IPLItem> IplList{
            get { return _iplList; }
            set { _iplList = value; }
        }
    }
}
